﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class UnitControl : UserControl, IControlRulable
    {
        private Unit innerUnit;

        public Unit InnerUnit
        {
            get { return innerUnit; }
            set { innerUnit = value; }
        }

        public UnitControl()
        {
            InitializeComponent();
            this.innerUnit = new Unit();
            this.FillControls();
        }

        public UnitControl(Unit unit):this()
        {
            this.innerUnit = unit;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.icDecimalDigits.Value = this.innerUnit.DecimalDigits;
                this.dcInterval.Value = this.innerUnit.Interval;
                this.dcMaximumValue.Value = this.innerUnit.MaximumValue;
                this.dcMinimumValue.Value = this.innerUnit.MinimumValue;
                this.strName.Value = this.innerUnit.Name;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerUnit.DecimalDigits = this.icDecimalDigits.Value.Value;
                    this.innerUnit.Interval = this.dcInterval.Value.Value;
                    this.innerUnit.MaximumValue = this.dcMaximumValue.Value.Value;
                    this.innerUnit.MinimumValue = this.dcMinimumValue.Value.Value;
                    this.innerUnit.Name = this.strName.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UnitControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerUnit.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
